{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Sum of Even Numbers After Queries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sumEvenAfterQueries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #查询后的偶数和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给出一个整数数组&nbsp;<code>A</code>&nbsp;和一个查询数组&nbsp;<code>queries</code>。</p>\n",
    "\n",
    "<p>对于第&nbsp;<code>i</code>&nbsp;次查询，有&nbsp;<code>val =&nbsp;queries[i][0], index&nbsp;= queries[i][1]</code>，我们会把&nbsp;<code>val</code>&nbsp;加到&nbsp;<code>A[index]</code>&nbsp;上。然后，第&nbsp;<code>i</code>&nbsp;次查询的答案是 <code>A</code> 中偶数值的和。</p>\n",
    "\n",
    "<p><em>（此处给定的&nbsp;<code>index = queries[i][1]</code>&nbsp;是从 0 开始的索引，每次查询都会永久修改数组&nbsp;<code>A</code>。）</em></p>\n",
    "\n",
    "<p>返回所有查询的答案。你的答案应当以数组&nbsp;<code>answer</code>&nbsp;给出，<code>answer[i]</code>&nbsp;为第&nbsp;<code>i</code>&nbsp;次查询的答案。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]\n",
    "<strong>输出：</strong>[8,6,2,4]\n",
    "<strong>解释：</strong>\n",
    "开始时，数组为 [1,2,3,4]。\n",
    "将 1 加到 A[0] 上之后，数组为 [2,2,3,4]，偶数值之和为 2 + 2 + 4 = 8。\n",
    "将 -3 加到 A[1] 上之后，数组为 [2,-1,3,4]，偶数值之和为 2 + 4 = 6。\n",
    "将 -4 加到 A[0] 上之后，数组为 [-2,-1,3,4]，偶数值之和为 -2 + 4 = 2。\n",
    "将 2 加到 A[3] 上之后，数组为 [-2,-1,3,6]，偶数值之和为 -2 + 6 = 4。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li><code>1 &lt;= A.length &lt;= 10000</code></li>\n",
    "\t<li><code>-10000 &lt;= A[i] &lt;= 10000</code></li>\n",
    "\t<li><code>1 &lt;= queries.length &lt;= 10000</code></li>\n",
    "\t<li><code>-10000 &lt;= queries[i][0] &lt;= 10000</code></li>\n",
    "\t<li><code>0 &lt;= queries[i][1] &lt; A.length</code></li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [sum-of-even-numbers-after-queries](https://leetcode.cn/problems/sum-of-even-numbers-after-queries/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [sum-of-even-numbers-after-queries](https://leetcode.cn/problems/sum-of-even-numbers-after-queries/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4]\\n[[1,0],[-3,1],[-4,0],[2,3]]', '[1]\\n[[4,0]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        odd = even = 0\n",
    "        for num in nums:\n",
    "            if num % 2: odd += num\n",
    "            else:   even += num\n",
    "        \n",
    "        for i in range(len(queries)):\n",
    "            if nums[queries[i][1]] % 2:\n",
    "                if queries[i][0] % 2:\n",
    "                    odd -= nums[queries[i][1]]\n",
    "                    even += nums[queries[i][1]] + queries[i][0]\n",
    "                else:\n",
    "                    odd += queries[i][0]\n",
    "            else:\n",
    "                if queries[i][0] % 2:\n",
    "                    even -= nums[queries[i][1]]\n",
    "                    odd += nums[queries[i][1]] + queries[i][0]\n",
    "                else:\n",
    "                    even += queries[i][0]\n",
    "            nums[queries[i][1]] +=  queries[i][0]\n",
    "            queries[i] = even\n",
    "        return queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        s = 0\n",
    "        for x in nums:\n",
    "            if x % 2 == 0:\n",
    "                s += x\n",
    "        i = 0\n",
    "        for val, idx in queries:\n",
    "            if nums[idx] % 2 == 0:\n",
    "                s -= nums[idx]\n",
    "            nums[idx] += val\n",
    "            if nums[idx] % 2 == 0:\n",
    "                s += nums[idx]\n",
    "            queries[i] = s\n",
    "            i += 1\n",
    "        return queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        odd = even = 0\n",
    "        for num in nums:\n",
    "            if num % 2: odd += num\n",
    "            else:   even += num\n",
    "        \n",
    "        for i in range(len(queries)):\n",
    "            if nums[queries[i][1]] % 2:\n",
    "                if queries[i][0] % 2:\n",
    "                    odd -= nums[queries[i][1]]\n",
    "                    even += nums[queries[i][1]] + queries[i][0]\n",
    "                else:\n",
    "                    odd += queries[i][0]\n",
    "            else:\n",
    "                if queries[i][0] % 2:\n",
    "                    even -= nums[queries[i][1]]\n",
    "                    odd += nums[queries[i][1]] + queries[i][0]\n",
    "                else:\n",
    "                    even += queries[i][0]\n",
    "            nums[queries[i][1]] +=  queries[i][0]\n",
    "            queries[i] = even\n",
    "        return queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        odd = even = 0\n",
    "        for num in nums:\n",
    "            if num % 2: odd += num\n",
    "            else:   even += num\n",
    "        \n",
    "        for i in range(len(queries)):\n",
    "            if nums[queries[i][1]] % 2:\n",
    "                if queries[i][0] % 2:\n",
    "                    odd -= nums[queries[i][1]]\n",
    "                    even += nums[queries[i][1]] + queries[i][0]\n",
    "                else:\n",
    "                    odd += queries[i][0]\n",
    "            else:\n",
    "                if queries[i][0] % 2:\n",
    "                    even -= nums[queries[i][1]]\n",
    "                    odd += nums[queries[i][1]] + queries[i][0]\n",
    "                else:\n",
    "                    even += queries[i][0]\n",
    "            nums[queries[i][1]] +=  queries[i][0]\n",
    "            queries[i] = even\n",
    "        return queries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        even = sum(i for i in nums if i % 2 == 0)\n",
    "        ans = []\n",
    "        for x,y in queries:\n",
    "            if nums[y] % 2 == 1 and x % 2 == 1:\n",
    "                nums[y] += x\n",
    "                even += nums[y]\n",
    "            elif nums[y] % 2 == 1 and x % 2 == 0:\n",
    "                nums[y] += x\n",
    "            elif nums[y] % 2 == 0 and x % 2 == 1:\n",
    "                even -= nums[y]\n",
    "                nums[y] += x\n",
    "            elif nums[y] % 2 == 0 and x % 2 == 0:\n",
    "                nums[y] += x\n",
    "                even += x\n",
    "            ans.append(even)\n",
    "        return ans\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        tmp=0\n",
    "        n=len(queries)\n",
    "        ans=[0]*n\n",
    "        A=nums\n",
    "        for a in A:\n",
    "            if a%2==0:\n",
    "                tmp+=a\n",
    "        #print(tmp)\n",
    "        for i,v in enumerate(queries):\n",
    "            pos=v[1]\n",
    "            val=v[0]\n",
    "            if val%2==A[pos]%2:\n",
    "                if A[pos]%2==0:\n",
    "                    tmp+=val\n",
    "                else:\n",
    "                    tmp+=val+A[pos]\n",
    "                A[pos]=A[pos]+val\n",
    "            elif val%2!=A[pos]%2:\n",
    "                if A[pos]%2==0:\n",
    "                    tmp-=A[pos]\n",
    "                A[pos]=A[pos]+val\n",
    "\n",
    "            ans[i]=tmp\n",
    "        return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        m,t=0,sum(x for x in nums if x%2==0)\n",
    "        ans=[]\n",
    "        qs=queries\n",
    "        for val,idx in qs:\n",
    "            if nums[idx]%2==0:\n",
    "                t-=nums[idx]\n",
    "            nums[idx]+=val\n",
    "            if nums[idx]%2==0:\n",
    "                t+=nums[idx]\n",
    "            ans.append(t)\n",
    "        return ans\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def sumEvenAfterQueries(self, A, queries):\n",
    "        S = sum(x for x in A if x % 2 == 0)\n",
    "        ans = []\n",
    "\n",
    "        for x, k in queries:\n",
    "            if A[k] % 2 == 0: S -= A[k]\n",
    "            A[k] += x\n",
    "            if A[k] % 2 == 0: S += A[k]\n",
    "            ans.append(S)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        result = []\n",
    "        sum_odd = 0\n",
    "        for num in nums:\n",
    "            if num%2 == 0:\n",
    "                sum_odd += num\n",
    "        for i in range(len(queries)):\n",
    "            val = queries[i][0]\n",
    "            index = queries[i][1]\n",
    "            last_num = nums[index]\n",
    "            nums[index] += val\n",
    "            if last_num%2 == 0:\n",
    "                if nums[index]%2 == 0:\n",
    "                    sum_odd = sum_odd-last_num+nums[index]\n",
    "                    result.append(sum_odd)\n",
    "                else:\n",
    "                    sum_odd = sum_odd-last_num\n",
    "                    result.append(sum_odd)\n",
    "            else:\n",
    "                if nums[index]%2 == 0:\n",
    "                    sum_odd = sum_odd+nums[index]\n",
    "                    result.append(sum_odd)\n",
    "                else:\n",
    "                    result.append(sum_odd)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        cur_sum = 0\n",
    "        for num in nums:\n",
    "            if(num % 2 == 0):\n",
    "                cur_sum += num\n",
    "        result = []\n",
    "        for val, index in queries:\n",
    "            if(nums[index] % 2 == 0):\n",
    "                cur_sum -= nums[index]\n",
    "            nums[index] += val \n",
    "            if(nums[index] % 2 == 0):\n",
    "                cur_sum += nums[index]\n",
    "            result.append(cur_sum)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        res = []\n",
    "\n",
    "        init_double_sum = sum([i for i in nums if i % 2 == 0])\n",
    "        \n",
    "        for i in queries:\n",
    "            val = i[0]\n",
    "            index = i[1]\n",
    "            # nums[index] += val\n",
    "\n",
    "            # 如果原来是单，新增是单，修改后变成双\n",
    "            if nums[index] % 2 == 1 and val % 2 == 1:\n",
    "                nums[index] += val\n",
    "                init_double_sum = init_double_sum + nums[index]\n",
    "                res.append(init_double_sum)\n",
    "\n",
    "            # 如果原来是单，新增是双，修改后变成单\n",
    "            elif nums[index] % 2 == 1 and val % 2 == 0:\n",
    "                nums[index] += val\n",
    "                res.append(init_double_sum)\n",
    "\n",
    "            # 如果原来是双，新增是单，修改后变成单\n",
    "            elif nums[index] % 2 == 0 and val % 2 == 1:\n",
    "                init_double_sum = init_double_sum - nums[index]\n",
    "                res.append(init_double_sum)\n",
    "                nums[index] += val\n",
    "                \n",
    "            # 如果原来是双，新增是双，修改后变成双\n",
    "            elif nums[index] % 2 == 0 and val % 2 == 0:\n",
    "   \n",
    "                init_double_sum = init_double_sum + val\n",
    "                res.append(init_double_sum)\n",
    "                nums[index] += val\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], qs: List[List[int]]) -> List[int]:\n",
    "        m, t = 0, sum(x for x in nums if x % 2 == 0)\n",
    "        ans = []\n",
    "        for val, idx in qs:\n",
    "            if nums[idx] % 2 == 0:\n",
    "                t -= nums[idx]\n",
    "            nums[idx] += val\n",
    "            if nums[idx] % 2 == 0:\n",
    "                t += nums[idx]\n",
    "            ans.append(t)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        sum = 0\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                sum += i\n",
    "        res = []\n",
    "        for j in queries:\n",
    "            nums[j[1]] += j[0]\n",
    "            if j[0] % 2 == 0:\n",
    "                if nums[j[1]] % 2 == 0:\n",
    "                    sum += j[0]\n",
    "                else:\n",
    "                    pass\n",
    "            elif j[0] % 2 != 0:\n",
    "                if nums[j[1]] % 2 == 0:\n",
    "                    sum += nums[j[1]]\n",
    "                else:\n",
    "                    sum -= (nums[j[1]] - j[0])\n",
    "\n",
    "            res.append(sum)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        l = len(queries)\n",
    "        res = [0] * l\n",
    "        even_sum = sum(x for x in nums if x % 2 == 0)  # 计算初始偶数和\n",
    "\n",
    "        for i in range(l):\n",
    "            val = queries[i][0]\n",
    "            index = queries[i][1]\n",
    "            original_val = nums[index]\n",
    "            nums[index] += val\n",
    "\n",
    "            # 更新偶数和\n",
    "            if original_val % 2 == 0:\n",
    "                even_sum -= original_val\n",
    "            if nums[index] % 2 == 0:\n",
    "                even_sum += nums[index]\n",
    "\n",
    "            res[i] = even_sum\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        cur_sum = 0\n",
    "        for num in nums:\n",
    "            if(num % 2 == 0):\n",
    "                cur_sum += num\n",
    "        result = []\n",
    "        for val, index in queries:\n",
    "            if(nums[index] % 2 == 0):\n",
    "                cur_sum -= nums[index]\n",
    "            nums[index] += val \n",
    "            if(nums[index] % 2 == 0):\n",
    "                cur_sum += nums[index]\n",
    "            result.append(cur_sum)\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        base = sum([x for x in nums if x % 2 == 0])\n",
    "        ans =[]\n",
    "        for val, idx in queries:\n",
    "            if nums[idx] % 2 == 0:\n",
    "                base -= nums[idx]\n",
    "            nums[idx] += val\n",
    "            if nums[idx] % 2 == 0:\n",
    "                base += nums[idx]\n",
    "            ans.append(base)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        add = 0\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                add += i\n",
    "        res = []\n",
    "        for l in queries:\n",
    "            if nums[l[1]] % 2 == 0:\n",
    "                add -= nums[l[1]]\n",
    "            nums[l[1]] += l[0]\n",
    "            if nums[l[1]] % 2 == 0:\n",
    "                add += nums[l[1]]\n",
    "            res.append(add)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        S = sum(x for x in A if x % 2 == 0)\n",
    "        ans = []\n",
    "\n",
    "        for x, k in queries:\n",
    "            if A[k] % 2 == 0: S -= A[k]\n",
    "            A[k] += x\n",
    "            if A[k] % 2 == 0: S += A[k]\n",
    "            ans.append(S)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        n = len(queries)\n",
    "        res = [0] * n\n",
    "        sums = 0\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                sums += i\n",
    "        \n",
    "        for i in range(n):\n",
    "            val, idx = queries[i][0], queries[i][1]\n",
    "            if nums[idx] % 2 == 0 and val % 2 ==0:\n",
    "                sums += val\n",
    "            if nums[idx] % 2 != 0 and val % 2 != 0:\n",
    "                sums += nums[idx] + val\n",
    "            if nums[idx] % 2 == 0 and val % 2 != 0:\n",
    "                sums -= nums[idx]\n",
    "            res[i] = sums\n",
    "            nums[idx] += val\n",
    "        return res\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(nums):\n",
    "    s=0\n",
    "    for v in nums:\n",
    "        if v %2 == 0:\n",
    "            s+=v\n",
    "    return s\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums, queries):\n",
    "        res = []\n",
    "        s = func1(nums)\n",
    "        for v in queries:\n",
    "            val = v[0]\n",
    "            idx = v[1]\n",
    "            temp = nums[idx]\n",
    "            nums[idx] += val\n",
    "            if temp % 2 != 0 and nums[idx] % 2 == 0:\n",
    "                s += nums[idx]\n",
    "\n",
    "            elif temp %2 ==0 and nums[idx] % 2 == 0:\n",
    "                s+=val\n",
    "            elif temp % 2 ==0 and nums[idx] %2 !=0:\n",
    "                s-=temp\n",
    "            res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        S = sum(i for i in nums if i % 2 == 0)\n",
    "        ans = []\n",
    "        for x, k in queries:\n",
    "            if nums[k] % 2 == 0:\n",
    "                S -= nums[k]\n",
    "            nums[k] += x\n",
    "            if nums[k] % 2 == 0:\n",
    "                S += nums[k]\n",
    "            ans.append(S)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        total = sum([num if num % 2 == 0 else 0 for num in nums])\n",
    "        ans = []\n",
    "        for val, idx in queries:\n",
    "            new_num = val + nums[idx]\n",
    "            if new_num % 2 == 0:\n",
    "                if nums[idx] % 2 == 0:\n",
    "                    total += val\n",
    "                else:\n",
    "                    total += new_num\n",
    "            else:\n",
    "                if nums[idx] % 2 == 0:\n",
    "                    total -= nums[idx]\n",
    "            nums[idx] = new_num\n",
    "            ans.append(total)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        base = sum([x for x in nums if x % 2 == 0])\n",
    "        ans = []\n",
    "        for val, idx in queries:\n",
    "            ori = nums[idx]\n",
    "            nums[idx] += val\n",
    "            now = nums[idx]\n",
    "            extra = (now if now % 2 == 0 else 0) - (ori if ori % 2 == 0 else 0)\n",
    "            base += extra\n",
    "            ans.append(base)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        answer = []\n",
    "        remain = []\n",
    "        trace = [0] * len(nums)\n",
    "        even_sum = 0\n",
    "        for n in range(len(nums)):\n",
    "            remain.append(nums[n] % 2)\n",
    "            if remain[n] == 0:\n",
    "                even_sum += nums[n]\n",
    "                trace[n] = nums[n]\n",
    "\n",
    "        for i in range(len(queries)):\n",
    "            index = queries[i][1]\n",
    "            nums[index] += queries[i][0]\n",
    "            if ( nums[index] % 2 ) == 0:\n",
    "                even_sum += ( nums[index] - trace[index] )\n",
    "                trace[index] = nums[index]\n",
    "            else:\n",
    "                even_sum -= trace[index]\n",
    "                trace[index] = 0\n",
    "            answer.append(even_sum)\n",
    "\n",
    "        return answer\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], qs: List[List[int]]) -> List[int]:\n",
    "        m, t = 0, sum(x for x in nums if x % 2 == 0)\n",
    "        ans = []\n",
    "        for val, idx in qs:\n",
    "            if nums[idx] % 2 == 0:\n",
    "                t -= nums[idx]\n",
    "            nums[idx] += val\n",
    "            if nums[idx] % 2 == 0:\n",
    "                t += nums[idx]\n",
    "            ans.append(t)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(nums):\n",
    "    s=0\n",
    "    for v in nums:\n",
    "        if v %2 == 0:\n",
    "            s+=v\n",
    "    return s\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums, queries):\n",
    "        res = []\n",
    "        s = func1(nums)\n",
    "        for val,idx in queries:\n",
    "            temp = nums[idx]\n",
    "            nums[idx] += val\n",
    "            if temp % 2 != 0 and nums[idx] % 2 == 0:\n",
    "                s += nums[idx]\n",
    "            elif temp %2 ==0 and nums[idx] % 2 == 0:\n",
    "                s+=val\n",
    "            elif temp % 2 ==0 and nums[idx] %2 !=0:\n",
    "                s-=temp\n",
    "            res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        sum_even = sum(x for x in nums if x % 2 == 0)\n",
    "        ans = [0] * len(queries)\n",
    "        for i, (v, idx) in enumerate(queries):\n",
    "            if nums[idx] % 2 == 0:\n",
    "                if v % 2 == 0:\n",
    "                    sum_even += v\n",
    "                else:\n",
    "                    sum_even -= nums[idx]    \n",
    "            else:\n",
    "                if v % 2:\n",
    "                    sum_even += nums[idx] + v\n",
    "            ans[i] = sum_even\n",
    "            nums[idx] += v\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        m = len(queries)\n",
    "        t = sum(x for x in nums if x % 2 == 0)\n",
    "        ans = [0] * m\n",
    "\n",
    "        for i in range(m):\n",
    "            val, idx = queries[i][0], queries[i][1]\n",
    "\n",
    "            if nums[idx] % 2 == 0:\n",
    "                t -= nums[idx]\n",
    "\n",
    "            nums[idx] += val\n",
    "\n",
    "            if nums[idx] % 2 == 0:\n",
    "                t += nums[idx]\n",
    "\n",
    "            ans[i] = t\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(nums):\n",
    "    s=0\n",
    "    for v in nums:\n",
    "        if v %2 == 0:\n",
    "            s+=v\n",
    "    return s\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums, queries):\n",
    "        res = []\n",
    "        s = func1(nums)\n",
    "        for val,idx in queries:\n",
    "            temp = nums[idx]\n",
    "            nums[idx] += val\n",
    "            if temp % 2 != 0 and nums[idx] % 2 == 0:\n",
    "                s += nums[idx]\n",
    "            elif temp %2 ==0 and nums[idx] % 2 == 0:\n",
    "                s+=val\n",
    "            elif temp % 2 ==0 and nums[idx] %2 !=0:\n",
    "                s-=temp\n",
    "            res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        n = len(queries)\n",
    "        final = []\n",
    "        total = 0\n",
    "        for item in nums:\n",
    "            if item % 2 == 0:\n",
    "                total += item\n",
    "        for i in range(n):\n",
    "            ori = nums[queries[i][1]]\n",
    "            nums[queries[i][1]] += queries[i][0]\n",
    "            if ori % 2 == 0:\n",
    "                total -= ori\n",
    "            if nums[queries[i][1]] % 2 == 0:\n",
    "                total += nums[queries[i][1]]\n",
    "            final.append(total)\n",
    "        return final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def func1(nums):\n",
    "    s=0\n",
    "    for v in nums:\n",
    "        if v %2 == 0:\n",
    "            s+=v\n",
    "    return s\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums, queries):\n",
    "        res = []\n",
    "        s = func1(nums)\n",
    "        for v in queries:\n",
    "            val = v[0]\n",
    "            idx = v[1]\n",
    "            temp = nums[idx]\n",
    "            nums[idx] += val\n",
    "            if temp % 2 != 0 and nums[idx] % 2 == 0:\n",
    "                s += nums[idx]\n",
    "            elif temp %2 ==0 and nums[idx] % 2 == 0:\n",
    "                s+=val\n",
    "            elif temp % 2 ==0 and nums[idx] %2 !=0:\n",
    "                s-=temp\n",
    "            res.append(s)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        base = sum([x for x in nums if x % 2 == 0])\n",
    "        ans =[]\n",
    "        for val, idx in queries:\n",
    "            if nums[idx] % 2 == 0:\n",
    "                base -= nums[idx]\n",
    "            nums[idx] += val\n",
    "            if nums[idx] % 2 == 0:\n",
    "                base += nums[idx]\n",
    "            ans.append(base)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        even_sum = 0\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            if num % 2 == 0:\n",
    "                even_sum += num\n",
    "        \n",
    "        for query in queries:\n",
    "            if nums[query[1]] % 2 == 0:\n",
    "                even_sum -= nums[query[1]]\n",
    "            nums[query[1]] += query[0]\n",
    "            if nums[query[1]] % 2 == 0:\n",
    "                even_sum += nums[query[1]]\n",
    "            res.append(even_sum)\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        # 初始偶数和\n",
    "        even_sum = sum(i for i in nums if i & 1 == 0)\n",
    "\n",
    "        res = []\n",
    "        for v,i in queries:\n",
    "            if nums[i] & 1 == 0:\n",
    "                even_sum -= nums[i]\n",
    "            nums[i] += v\n",
    "            if nums[i] & 1 == 0:\n",
    "                even_sum += nums[i]\n",
    "            res.append(even_sum)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        # 初始偶数和\n",
    "        even_sum = sum(i for i in nums if i & 1 == 0)\n",
    "\n",
    "        res = []\n",
    "        for v,i in queries:\n",
    "            ori = nums[i] & 1\n",
    "            add = v & 1\n",
    "            # 根据奇偶性进行不同操作\n",
    "            match ori, add:\n",
    "                case 1, 1:\n",
    "                    even_sum += nums[i] + v\n",
    "                case 0, 1:\n",
    "                    even_sum -= nums[i]\n",
    "                case 0, 0:\n",
    "                    even_sum += v\n",
    "\n",
    "            nums[i] += v\n",
    "            res.append(even_sum)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def sumEvenAfterQueries(self, A, queries):\n",
    "        S = sum(x for x in A if x % 2 == 0)\n",
    "        ans = []\n",
    "\n",
    "        for x, k in queries:\n",
    "            if A[k] % 2 == 0: S -= A[k]\n",
    "            A[k] += x\n",
    "            if A[k] % 2 == 0: S += A[k]\n",
    "            ans.append(S)\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        total, ans = sum(num for num in nums if num % 2 == 0), []\n",
    "        for val, idx in queries:\n",
    "            num, num1 = nums[idx], nums[idx] + val\n",
    "            if num % 2 == 0: total -= num\n",
    "            if num1 % 2 == 0: total += num1\n",
    "            ans.append(total)\n",
    "            nums[idx] = num1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        even_sum = sum(num for num in nums if num % 2 == 0)\n",
    "        ans = []\n",
    "        \n",
    "        for x, y in queries:\n",
    "            if nums[y] % 2 == 0:\n",
    "                even_sum -= nums[y]\n",
    "            \n",
    "            nums[y] += x\n",
    "            \n",
    "            if nums[y] % 2 == 0:\n",
    "                even_sum += nums[y]\n",
    "            \n",
    "            ans.append(even_sum)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]: \n",
    "        n = len(nums) \n",
    "        tot = 0 \n",
    "        for i in range(n):\n",
    "            if nums[i] % 2 == 0:\n",
    "                tot += nums[i] \n",
    "        ans = []\n",
    "        for val, idx in queries:\n",
    "            pre = nums[idx] \n",
    "            if pre % 2 == 0:\n",
    "                tot -= pre \n",
    "            nums[idx] += val \n",
    "            if nums[idx] % 2 == 0:\n",
    "                tot += nums[idx] \n",
    "            ans.append(tot)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "\n",
    "        ans = [0] * len(queries)\n",
    "        nums_sum = 0\n",
    "        for ii in range(len(nums)):\n",
    "            if(nums[ii] % 2 == 0):\n",
    "                nums_sum += nums[ii]\n",
    "        print(nums_sum)\n",
    "\n",
    "        for ll in range(len(queries)):\n",
    "            val = queries[ll][0]\n",
    "            index = queries[ll][1]\n",
    "\n",
    "            if(nums[index] % 2 == 0):\n",
    "                if ((nums[index] + val) % 2 == 0):\n",
    "                    nums_sum += val\n",
    "                else:\n",
    "                    nums_sum -= nums[index]\n",
    "            else:\n",
    "                if((nums[index] + val) % 2 == 0):\n",
    "                    nums_sum += val + nums[index]\n",
    "            nums[index] += val\n",
    "            ans[ll] = nums_sum\n",
    "            \n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def sumEvenAfterQueries(self, A, queries):\n",
    "        S = sum(x for x in A if x % 2 == 0)\n",
    "        ans = []\n",
    "        for x, k in queries:\n",
    "            if A[k] % 2 == 0: S -= A[k]\n",
    "            A[k] += x\n",
    "            if A[k] % 2 == 0: S += A[k]\n",
    "            ans.append(S)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        ans = []\n",
    "        sum_ou = 0\n",
    "        sum_ji = 0\n",
    "        for i in nums:\n",
    "            if i % 2 == 0:\n",
    "                sum_ou += i\n",
    "            else:\n",
    "                sum_ji += i\n",
    "        for j in queries:\n",
    "            if nums[j[1]] % 2 ==0:\n",
    "                if j[0] % 2 == 0:\n",
    "                    sum_ou += j[0]\n",
    "                else:\n",
    "                    sum_ji = sum_ji + nums[j[1]] + j[0]\n",
    "                    sum_ou = sum_ou - nums[j[1]]\n",
    "            else:\n",
    "                if j[0] % 2 == 0:\n",
    "                    sum_ji = sum_ji + j[0]\n",
    "                else:\n",
    "                    sum_ou = sum_ou + nums[j[1]] + j[0]\n",
    "                    sum_ji = sum_ji - nums[j[1]]\n",
    "            ans.append(sum_ou)\n",
    "            nums[j[1]] += j[0]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        ans = []\n",
    "        total = sum([i for i in nums if i % 2 == 0])\n",
    "        for i, val in enumerate(nums):\n",
    "            val = queries[i][0]\n",
    "            idx = queries[i][1]\n",
    "            if nums[idx]  % 2 == 0:\n",
    "                if (nums[idx] + val) % 2 == 0:\n",
    "                    total += val\n",
    "                else:\n",
    "                    total -= nums[idx]\n",
    "                nums[idx] += val\n",
    "            else:\n",
    "                if (nums[idx] + val) % 2 == 0:\n",
    "                    total += nums[idx] + val\n",
    "                nums[idx] += val\n",
    "            ans.append(total)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        odd = sum(i for i in nums if i % 2 == 1)\n",
    "        even = sum(i for i in nums if i % 2 == 0)\n",
    "        ans = []\n",
    "        for x,y in queries:\n",
    "            if nums[y] % 2 == 1 and x % 2 == 1:\n",
    "                odd -= nums[y]\n",
    "                nums[y] += x\n",
    "                even += nums[y]\n",
    "\n",
    "            elif nums[y] % 2 == 1 and x % 2 == 0:\n",
    "                nums[y] += x\n",
    "                odd += x\n",
    "            elif nums[y] % 2 == 0 and x % 2 == 1:\n",
    "                even -= nums[y]\n",
    "                nums[y] += x\n",
    "                odd += nums[y]\n",
    "            elif nums[y] % 2 == 0 and x % 2 == 0:\n",
    "                nums[y] += x\n",
    "                even += x\n",
    "            ans.append(even)\n",
    "        return ans\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        ans = []\n",
    "        val = sum([n for n in nums if n % 2 == 0])\n",
    "        pos = [i for i in range(len(nums)) if nums[i] % 2 == 0]\n",
    "        for q in queries:\n",
    "            old = nums[q[1]]\n",
    "            nums[q[1]] += q[0]\n",
    "            if q[1] not in pos:\n",
    "                if nums[q[1]] % 2 == 0:\n",
    "                    val += nums[q[1]]\n",
    "                    pos.append(q[1])\n",
    "                ans.append(val)\n",
    "            else:\n",
    "                if nums[q[1]] % 2 == 0:\n",
    "                    val += q[0]\n",
    "                else:\n",
    "                    val -= old\n",
    "                    pos.remove(q[1])\n",
    "                ans.append(val)\n",
    "        return ans\n",
    "            \n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def sumEvenAfterQueries(self, A, queries):\n",
    "        S = sum(x for x in A if x % 2 == 0)\n",
    "        ans = []\n",
    "\n",
    "        for x, k in queries:\n",
    "            if A[k] % 2 == 0: S -= A[k]\n",
    "            A[k] += x\n",
    "            if A[k] % 2 == 0: S += A[k]\n",
    "            ans.append(S)\n",
    "\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def sumEvenAfterQueries(self, A, queries):\n",
    "        S = sum(x for x in A if x % 2 == 0)\n",
    "        ans = []\n",
    "\n",
    "        for x, k in queries:\n",
    "            if A[k] % 2 == 0: S -= A[k]\n",
    "            A[k] += x\n",
    "            if A[k] % 2 == 0: S += A[k]\n",
    "            ans.append(S)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        res=[]\n",
    "        ss=sum(nums)\n",
    "        s=0\n",
    "        for j in nums:\n",
    "                if j%2==0:\n",
    "                    s+=j\n",
    "        res.append(s)\n",
    "        for i in queries:\n",
    "            if nums[i[1]]%2==0 and i[0]%2==0:\n",
    "                res.append(res[-1]+i[0])\n",
    "                nums[i[1]]+=i[0]\n",
    "            elif nums[i[1]]%2!=0 and i[0]%2==0:\n",
    "                \n",
    "                res.append(res[-1])\n",
    "                nums[i[1]]+=i[0]\n",
    "            elif nums[i[1]]%2==0 and i[0]%2!=0:\n",
    "                \n",
    "                res.append(res[-1]-nums[i[1]])\n",
    "                nums[i[1]]+=i[0]\n",
    "            elif nums[i[1]]%2!=0 and i[0]%2!=0:\n",
    "                \n",
    "                \n",
    "                nums[i[1]]+=i[0]\n",
    "                res.append(res[-1]+nums[i[1]])\n",
    "        return res[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        ans=[]\n",
    "        res=0\n",
    "        for each in nums:\n",
    "            if each%2==0:\n",
    "                res+=each\n",
    "        for i,j in queries:\n",
    "            if i%2==0:\n",
    "                nums[j]+=i\n",
    "                res+=int(((-1)**(nums[j])+1)/2)*i\n",
    "                ans.append(res)\n",
    "            else:\n",
    "                if nums[j]%2==0:\n",
    "                    res-=nums[j]\n",
    "                    nums[j]+=i\n",
    "                    ans.append(res)\n",
    "                else:\n",
    "                    nums[j]+=i\n",
    "                    res+=nums[j]\n",
    "                    ans.append(res)\n",
    "        return ans\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        res = []\n",
    "        cur_sum = sum([i for i in A if i%2 == 0])\n",
    "        for querie in queries:\n",
    "            val,idx = querie\n",
    "            raw = A[idx]\n",
    "            new = raw+val\n",
    "            A[idx] = new\n",
    "            raw_is_even = raw % 2 == 0\n",
    "            new_is_even = new % 2 == 0\n",
    "            if new_is_even:\n",
    "                if raw_is_even:\n",
    "                    cur_sum += (new-raw)\n",
    "                else:\n",
    "                    cur_sum += new\n",
    "            else:\n",
    "                if raw_is_even:\n",
    "                    cur_sum -= raw\n",
    "                else:\n",
    "                    pass \n",
    "                    \n",
    "            res.append(cur_sum)\n",
    "            \n",
    "        return(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        ans = []\n",
    "        even = sum(num if num % 2 == 0 else 0 for num in nums)\n",
    "        for query, i in queries:\n",
    "            if nums[i] % 2 == 0:\n",
    "                even -= nums[i]\n",
    "\n",
    "            #nums[query[1]] += query[0]\n",
    "            #nums[query[1]] = sum(num if num % 2 == 0 else 0 for num in nums)\n",
    "            nums[i] += query\n",
    "            if nums[i] % 2 == 0:\n",
    "                even += nums[i]\n",
    "            ans.append(even)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        s = 0\n",
    "        for x in nums:\n",
    "            if x % 2 == 0:\n",
    "                s += x\n",
    "        ans = []\n",
    "        for val, idx in queries:\n",
    "            if nums[idx] % 2 == 0:\n",
    "                s -= nums[idx]\n",
    "            nums[idx] += val\n",
    "            if nums[idx] % 2 == 0:\n",
    "                s += nums[idx]\n",
    "            ans.append(s)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        m = len(queries)\n",
    "        #print(m,n)\n",
    "\n",
    "\n",
    "        for i in range(m):\n",
    "            var,index= queries[i][0],queries[i][1]\n",
    "            #print(var,index)\n",
    "        \n",
    "        l1= []\n",
    "        #oushu_sum = [num for num in nums if num %2 ==0]\n",
    "        #print(oushu_sum)\n",
    "        oushu_sum = sum([i for i in nums if i %2 == 0])\n",
    "        #print(oushu_sum)\n",
    "\n",
    "        for i in queries:\n",
    "           \n",
    "            if nums[i[1]] % 2 ==0:\n",
    "                oushu_sum-= nums[i[1]]\n",
    "            nums[i[1]] += i[0]\n",
    "\n",
    "            if nums[i[1]] % 2 ==0:\n",
    "                oushu_sum+= nums[i[1]]\n",
    "            l1.append(oushu_sum)\n",
    "        return l1\n",
    "              \n",
    "      \n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef sumEvenAfterQueries(self, nums: list[int], queries: list[list[int]]) -> list[int]:\n",
    "\t\tans = list()\n",
    "\t\tsum=0#数组的偶数和\n",
    "\t\tfor i in nums:\n",
    "\t\t\tif i%2==0:\n",
    "\t\t\t\tsum+=i\n",
    "\t\tfor querie in queries:\n",
    "\t\t\tindex=querie[1]\n",
    "\t\t\tval=querie[0]\n",
    "\t\t\tif nums[index]%2==0:#之前的数是偶数\n",
    "\t\t\t\ttemp=nums[index]\n",
    "\t\t\t\tnums[index]+=val#加val\n",
    "\t\t\t\tif nums[index]%2==0:#加上val之后还是偶数,直接往sum+=val\n",
    "\t\t\t\t\tsum+=val\n",
    "\t\t\t\telse:#加上之后不是偶数了\n",
    "\t\t\t\t\tsum-=temp#sum去除nums[index]\n",
    "\t\t\telse:#之前不是偶数\n",
    "\t\t\t\tnums[index]+=val\n",
    "\t\t\t\tif nums[index]%2==0:#加上后变成偶数了\n",
    "\t\t\t\t\tsum+=nums[index]#sum加上这个新出现的偶数\n",
    "\t\t\tans.append(sum)#不要忘记加入返回的数组哦\n",
    "\t\treturn ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        ans = []\n",
    "        even = sum(num for num in nums if num % 2 == 0)\n",
    "        for val, ind in queries:\n",
    "            if nums[ind] % 2 == 0:\n",
    "                even -= nums[ind]\n",
    "            nums[ind] += val\n",
    "            if nums[ind] % 2 == 0:\n",
    "                even += nums[ind]\n",
    "            ans.append(even)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:\n",
    "        odd = 0\n",
    "        n = len(nums)\n",
    "        for num in nums:\n",
    "            if num % 2 == 0:\n",
    "                odd += num\n",
    "        answers = list()\n",
    "        for val ,index in queries:\n",
    "            cur = nums[index]\n",
    "            if cur % 2 == 0:\n",
    "                odd -= cur\n",
    "            if (cur+val) % 2 == 0:\n",
    "                odd += cur + val\n",
    "            nums[index] = cur+val \n",
    "            answers.append(odd)\n",
    "        return answers"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
